package com.junxonline.plat.controller.menu;

import com.junxonline.plat.dao.vo.*;
import com.junxonline.common.dao.vo.*;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.controller.base.BaseController;
import com.junxonline.plat.exception.ErrorCodeEnum;
import com.junxonline.plat.service.menu.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JunX
 * @ClassName: MenuController
 * @Description: 菜单控制器
 */

@RestController
@RequestMapping(value = "/admin/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    /**
     * 获取导航菜单数据
     * @return
     */
    @RequestMapping(value = "/getNavMenu")
    public ResultVO getNavMenu() {
        Long userId = getUserId();
        return getReturnSuccess(menuService.getNavMenu(userId));
    }

    /**
     * 获取按钮数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getButton")
    public ResultVO getButton(@RequestBody MenuVO vo) {

        if (CommonUtils.isEmpty(vo.getMenuUrl())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long userId = getUserId();
        return getReturnSuccess(menuService.getButton(vo.getMenuUrl(), userId));
    }

    /**
     * 获取菜单树数据
     * @return
     */
    @RequestMapping(value = "/getMenuTree")
    public ResultVO getMenuTree() {
        return getReturnSuccess(menuService.getMenuTree());
    }

    /**
     * 获取父级菜单树数据
     * @return
     */
    @RequestMapping(value = "/getParentMenuTree")
    public ResultVO getParentMenuTree() {
        return getReturnSuccess(menuService.getParentMenuTree());
    }

    /**
     * 获取菜单数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getMenu")
    public ResultVO getMenu(@RequestBody QueryMenuVO vo) {

        if (CommonUtils.isEmpty(vo.getMenuId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        return getReturnSuccess(menuService.getMenu(vo));
    }

    /**
     * 筛选查询菜单数据
     * @param page
     * @param vo
     * @return
     */
    @RequestMapping(value = "/queryMenu")
    public PageResultVO queryMenu(PageVO page, QueryMenuVO vo) {
        return getReturnSuccess(menuService.queryMenu(page, vo));
    }


    /**
     * 添加菜单
     * @param vo
     * @return
     */
    @RequestMapping(value = "/addMenu")
    public ResultVO addMenu(@RequestBody MenuVO vo) {

        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getMenuType()) || CommonUtils.isEmpty(vo.getMenuName()) ||
            CommonUtils.isEmpty(vo.getMenuPermission()) || CommonUtils.isEmpty(vo.getMenuSort()) || CommonUtils.isEmpty(vo.getMenuStatus())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = menuService.addMenu(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }

    /**
     * 修改菜单
     * @param vo
     * @return
     */
    @RequestMapping(value = "/editMenu")
    public ResultVO editMenu(@RequestBody MenuVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getMenuId()) || CommonUtils.isEmpty(vo.getMenuType()) || CommonUtils.isEmpty(vo.getMenuName()) ||
                CommonUtils.isEmpty(vo.getMenuPermission()) || CommonUtils.isEmpty(vo.getMenuSort()) || CommonUtils.isEmpty(vo.getMenuStatus())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        // 判断选择的父菜单是否是自己
        if (vo.getMenuId().toString().equals(vo.getMenuParentId())) {
            return getReturnError(ErrorCodeEnum.MENU_PARENT_NOT_SELF);
        }

        boolean success = menuService.editMenu(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }


    /**
     * 删除菜单
     * @param vo
     * @return
     */
    @RequestMapping(value = "/delMenu")
    public ResultVO delMenu(@RequestBody MenuIdsVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getMenuIds())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long[] menuIdsArray = vo.getMenuIds();

        boolean success = menuService.delMenu(menuIdsArray);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }

}
